﻿using System;
using System.Windows;
using System.Windows.Media;
using System.Windows.Interop;
using Microsoft.Windows.Controls.Ribbon;
using System.Windows.Input;
using System.Windows.Media.Animation;
using System.Windows.Controls;
using WPFMitsuControls;
using System.Windows.Controls.Primitives;
using System.Runtime.InteropServices;
using System.Windows.Data;
using Microsoft.Win32;
using Guitar.Vues;
using System.Diagnostics;
using System.Windows.Threading;
using System.Xml.Serialization;
using System.IO;
using System.ComponentModel;

namespace Guitar
{
    /// (c) DAICHA Adil
    /// <summary>
    /// Logique d'interaction pour MainWindow.xaml
    /// </summary>

    public partial class MainWindow : Window
    {

        public ModeLivre vueLivre = new ModeLivre();
        public ModeNormal vuePage = new ModeNormal();
        public bool IsFullScreen = false;
        public bool isCachedMod = false; // Active la composition caché pour améliorer drastiquement les performances

        // Gestion des animations
        public MainWindow()
        {

            InitializeComponent();
            App.Pages.Add(new Modeleur());
            App.Pages.Add(new Modeleur());
            App.Pages.Add(new Modeleur());
            App.Pages.Add(new Modeleur());
            UpdateNumPage();
            App.Pages[App.ModeleurEnCours].Remplir(App.PartitionEnCours, App.PisteEnCours);
            App.Pages[App.ModeleurEnCours].RemplirPiste(App.PartitionEnCours, App.PisteEnCours);
            App.Pages[App.ModeleurEnCours].RemplirMesure(0, App.Partitions[App.PartitionEnCours].CountMesure(App.PisteEnCours), true);


        }


        /* préparation de la fenêtre */
        void OnLoaded(object sender, RoutedEventArgs e)
        {
            try
            {
                // Obtient le corps de la fenêtre pour une application WPF 
                IntPtr mainWindowPtr = new WindowInteropHelper(this).Handle;
                HwndSource mainWindowSrc = HwndSource.FromHwnd(mainWindowPtr);
                mainWindowSrc.CompositionTarget.BackgroundColor = System.Windows.Media.Color.FromArgb(0, 0, 0, 0);

                // Obtien la résolution du système hote
                System.Drawing.Graphics desktop = System.Drawing.Graphics.FromHwnd(mainWindowPtr);
                float DesktopDpiX = desktop.DpiX;
                float DesktopDpiY = desktop.DpiY;

                //  Création des marges
                SafeNativeMethods.MARGES margins = new SafeNativeMethods.MARGES();

                // on étend aero glass
                margins.epaisseurGauche = Convert.ToInt32(1 * (DesktopDpiX / 96));
                margins.epaisseurDroite = Convert.ToInt32(1 * (DesktopDpiX / 96));
                margins.epaisseurHaut = Convert.ToInt32(250 * (DesktopDpiX / 96));
                margins.epaisseurBas = Convert.ToInt32(1 * (DesktopDpiX / 96));

                int hr = SafeNativeMethods.DwmExtendFrameIntoClientArea(mainWindowSrc.Handle, ref margins);

                if (hr < 0)
                {
                    Console.WriteLine("Erreur MainWindows.cs");
                }


            }
            // Si windows ne supporte pas aero on paint en blanc
            catch (DllNotFoundException)
            {
                System.Windows.Application.Current.MainWindow.Background = System.Windows.Media.Brushes.White;
            }


            App.Pages[App.ModeleurEnCours].IsFirstPage = true;


            foreach (Modeleur m in App.Pages)
            {
                vueLivre.Livre.Items.Add(m);
            }


            Conteneur.Children.Clear();
            Conteneur.Children.Add(vueLivre);

        }


        public void AjoutLivre()
        {
            vuePage.panel.Children.Clear();
            foreach (Modeleur m in App.Pages)
            {
                vueLivre.Livre.Items.Add(m);
            }

        }


        public void AjoutNormal()
        {
            vueLivre.Livre.Items.Clear();
            foreach (Modeleur m in App.Pages)
            {
                vuePage.panel.Children.Add(m);
            }

        }

        /// <summary>
        /// Permet de mettre l'application en mode plein écran
        /// </summary>
        public void fullScreen()
        {
            if (!IsFullScreen)
            {
                Menu.MainRibbon.IsMinimized = true;
                this.WindowState = WindowState.Normal;
                this.WindowStyle = WindowStyle.None;
                this.WindowState = WindowState.Maximized;
                this.Topmost = true;
                IsFullScreen = true;

            }
            else
            {
                Menu.MainRibbon.IsMinimized = false;
                this.WindowStyle = WindowStyle.ThreeDBorderWindow;
                this.WindowState = WindowState.Normal;
                this.Topmost = false;
                IsFullScreen = false;
            }

        }


        public void UpdateNumPage()
        {

            int nbpage = App.Pages.Count;
            for (int i = 0; i < nbpage; i++)
            {
                App.Pages[i].lblNumPage.Content = (i + 1) +"/"+( App.Pages.Count );
            }
        }

        private void ScrollViewer_MouseEnter(object sender, MouseEventArgs e)
        {
            ScrollPiste.Width = 720;

        }

        private void ScrollViewer_MouseLeave(object sender, MouseEventArgs e)
        {
            ScrollPiste.Width = 100;
        }

        private void button1_Click(object sender, RoutedEventArgs e)
        {
            UseCachedComposition();
        }


        public void UseCachedComposition()
        {
            App.Pages[App.ModeleurEnCours].VuePiste.CacheMode = null;
            App.Pages[App.ModeleurEnCours].VuePiste.CacheMode = new BitmapCache();

        }

        public void RemoveCachedComposition()
        {
            App.Pages[App.ModeleurEnCours].VuePiste.CacheMode = null;

        }


        private void zoomSlider_PreviewMouseButtonClick(object sender, MouseButtonEventArgs e)
        {
            UseCachedComposition();
        }


        private void zoomSlider_PreviewMouseButtonUp(object sender, MouseButtonEventArgs e)
        {
            RemoveCachedComposition();
        }




    }


}

